package com.epta.service;

import com.epta.pojo.BasePojo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/8 0008.
 */
public class BaseService<T extends BasePojo> {
    @Autowired
    private Mapper<T> mapper;

    public T queryById(Object id){
        return this.mapper.selectByPrimaryKey(id);
    }

    public T queryOne(T example){
        return this.mapper.selectOne(example);
    }

    public List<T> queryAll(){
        return this.mapper.select(null);
    }

    public List<T> queryListByWhere(T example){
        return this.mapper.select(example);
    }

    public PageInfo<?> queryPageListByWhere(T example,Integer page,Integer rows){
        PageHelper.startPage(page,rows);
        List<T> list = this.mapper.select(example);
        return new PageInfo<>(list);
    }

    public Integer save(T t){
        Date date = new Date();
        t.setCreated(date);
        t.setUpdated(date);
        return this.mapper.insert(t);
    }

    public Integer update(T t){
        t.setUpdated(new Date());
        return this.mapper.updateByPrimaryKey(t);
    }

    public Integer updateSelective(T t){
        t.setUpdated(new Date());
        return this.mapper.updateByPrimaryKeySelective(t);
    }

    public boolean deleteById(Object id){
        return this.mapper.deleteByPrimaryKey(id) == 1;
    }

    public Integer deleteByIds(Class<T> clazz,String property,List<Object> list){
        Example example = new Example(clazz);
        example.createCriteria().andIn(property,list);
        return this.mapper.deleteByExample(example);
    }

    public Integer dleteByWhere(T example){
        return this.mapper.delete(example);
    }












}
